package com.qiaoyanqing.textdemoqyq7.AsyncTasks;

import android.os.AsyncTask;
import android.util.Log;

import com.qiaoyanqing.textdemoqyq7.Beans.WebBeans;
import com.qiaoyanqing.textdemoqyq7.CallBacks.Web2CB;
import com.qiaoyanqing.textdemoqyq7.Utils.MyHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/16.
 */

public class Web2AsyncTask extends AsyncTask<String,Void,List<WebBeans>> {

    private  List<WebBeans>mData;
    private Web2CB mWeb2CB;


    public Web2AsyncTask(Web2CB web2CB) {
        mWeb2CB = web2CB;

    }

    @Override
    protected List<WebBeans> doInBackground(String... strings) {
        String result= MyHttpUtils.getStringFromUrl(strings[0]);

        List<WebBeans>list=parseJson(result);

        return list;
    }

    private List<WebBeans> parseJson(String result) {
           mData=new ArrayList<>();
        try {
            JSONObject jsonObject=new JSONObject(result);
            JSONObject dataObj=jsonObject.optJSONObject("data");

             WebBeans beans=new WebBeans();
            beans.setId(dataObj.optString("id"));
            beans.setTitle(dataObj.optString("title"));
            beans.setSource(dataObj.optString("source"));
            beans.setWap_content(dataObj.optString("wap_content"));
            beans.setCreate_time(dataObj.optString("create_time"));
            beans.setAuthor(dataObj.optString("author"));
            beans.setWeiboUrl(dataObj.optString("weiboUrl"));

            mData.add(beans);

            Log.d("flag", "WEBVIEW  parseJson: "+mData.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return mData;
    }

    @Override
    protected void onPostExecute(List<WebBeans> beans) {
        super.onPostExecute(beans);
        if (beans != null) {
            mWeb2CB.senddata(beans);
        }
    }
}
